package com.sailing.study;

import com.sailing.study.tools.StringUtils;

/**
 * @author chenchao
 * @time 2023-05-12 16:08
 */
public class SobotApiDocBeanCreator {

    /**
     * 根据固定格式：属性名\t数据结构\t注释\n, 生成Bean
     *
     * 属性名：若含下划线，放在json映射注解里，java的属性使用驼峰
     *
     * "land_page_url\tString\t否\t着陆页 url\n" +
     * "land_page_title\tString\t否\t着陆页标题\n"
     * @param content
     * @return
     */
    public static String createBean(String content){
        StringBuilder result = new StringBuilder();
        String[] rowList = content.split("\n");
        String space = " ";
        String cl = "\n";
        for (String row : rowList) {
            String[] cells = row.split("\t", 3);

            String name = cells[0];

            String annoRow = "";
            if (name.contains("_")) {
                //
                annoRow = String.format("@JSONField(name = \"%s\")\n", name);

                String[] words = name.split("_");
                StringBuilder namesb = new StringBuilder();
                for (int i = 0; i < words.length; i++) {
                    if (i > 0) {
                        namesb.append(StringUtils.firstUpper(words[i]));
                    } else {
                        namesb.append(words[i]);
                    }
                }
                name = namesb.toString();
            }
            result.append(new StringBuilder().append("/**\n").append("*").append(space).append(cells[2]).append("\n*/\n")
                    .append(annoRow)
                            .append("private").append(space)
                    .append(cells[1]).append(space)
                    .append(name).append(";\n")
            );
        }
        return result.toString();
    }

}
